package com.cdvtc.fengchao.Servlet;

import com.cdvtc.fengchao.dao.GeneralDao;
import com.cdvtc.fengchao.model.OrderDetail;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/orderDetailManagementServlet")
public class OrderDetailServlet extends HttpServlet {

    private GeneralDao generalDao = new GeneralDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 查询订单细则数据
        List<Map<String, Object>> orderDetails = generalDao.getOrderDetails();
        System.out.println("查询到的订单细则数量: " + orderDetails.size());
        request.setAttribute("orderDetails", orderDetails);

        // 转发到JSP页面
        request.getRequestDispatcher("/admin/order_details.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if ("add".equals(action)) {
            // 添加订单细则
            int orderId = Integer.parseInt(request.getParameter("orderId"));
            int productId = Integer.parseInt(request.getParameter("productId"));
            int quantity = Integer.parseInt(request.getParameter("quantity"));
            double totalPrice = Double.parseDouble(request.getParameter("totalPrice"));
            int result = generalDao.addOrderDetail(orderId, productId, quantity, totalPrice);
            if (result > 0) {
                System.out.println("订单细则添加成功");
            } else {
                System.out.println("订单细则添加失败");
            }
        } else if ("update".equals(action)) {
            // 更新订单细则
            int id = Integer.parseInt(request.getParameter("id"));
            int orderId = Integer.parseInt(request.getParameter("orderId"));
            int productId = Integer.parseInt(request.getParameter("productId"));
            int quantity = Integer.parseInt(request.getParameter("quantity"));
            double totalPrice = Double.parseDouble(request.getParameter("totalPrice"));
            int result = generalDao.updateOrderDetail(id, orderId, productId, quantity, totalPrice);
            if (result > 0) {
                System.out.println("订单细则更新成功");
            } else {
                System.out.println("订单细则更新失败");
            }
        } else if ("delete".equals(action)) {
            // 删除订单细则
            int id = Integer.parseInt(request.getParameter("id"));
            int result = generalDao.deleteOrderDetail(id);
            if (result > 0) {
                System.out.println("订单细则删除成功");
            } else {
                System.out.println("订单细则删除失败");
            }
        }
        response.sendRedirect(request.getContextPath() + "/admin/orderDetailManagementServlet");
    }
}